
package Controlador;

import Controladores_Consultas.ControladorConsulCooperativa;
import Modelo.Choferes;
import Modelo.Cooperativa;
import Modelo.Listas;
import Vista.Mensajes;
import Vista.JFrameCooperativa;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.LinkedList;

public class ControladorCooperativa implements ActionListener,KeyListener
{

    private JFrameCooperativa formCooperativa;
    private Listas ListaCoop;

    public ControladorCooperativa(Listas LisCoop) {
    
        ListaCoop=LisCoop;  
        formCooperativa=new JFrameCooperativa();  
        formCooperativa.agregarListener(this);
        formCooperativa.setVisible(true);
        Enabled(true);
        
     //-------------------------------
        formCooperativa.getTxtRifC().addKeyListener(new KeyAdapter() 
        {
            public void keyTyped(KeyEvent e) 
            {
                rifKeyTyped(e);
            }

            public void keyPressed(KeyEvent e) {

                rifkeyPressed(e);
            }
        });
     //-------------------------------
        formCooperativa.getTxtNombre().addKeyListener(new KeyAdapter() 
        {
            public void keyTyped(KeyEvent e) 
            {
                nombreKeyTyped(e);
            }

            public void keyPressed(KeyEvent e) {

                nombrekeyPressed(e);
            }
        });
     //-------------------------------
        formCooperativa.getTxtTelefonoC().addKeyListener(new KeyAdapter() 
        {
            public void keyTyped(KeyEvent e) 
            {
                telefonoKeyTyped(e);
            }

            public void keyPressed(KeyEvent e) {

                telefonokeyPressed(e);
            }
        });

    }
     //-------------------------------
            //Enable
     //-------------------------------
    private void Enabled(boolean Status)
    {
        formCooperativa.getTxtRifC().setEnabled(!Status);
        formCooperativa.getTxtNombre().setEnabled(Status);
        formCooperativa.getTxtTelefonoC().setEnabled(Status);
    }
    
     //-------------------------------
            //Limpiar
     //-------------------------------
    private void Limpiar()
    {
        formCooperativa.getTxtRifC().setText("");
        formCooperativa.getTxtNombre().setText("");
        formCooperativa.getTxtTelefonoC().setText("");
    }
     //-------------------------------
            // Zona de eventos Typed
     //-------------------------------
    
            private void rifKeyTyped(KeyEvent e) 
            {
            }


            
            private void nombreKeyTyped(KeyEvent e) 
            {
            }


            
            private void telefonoKeyTyped(KeyEvent e) 
            {
            }
      //-------------------------------
            // Zona de eventos Pressed
     //-------------------------------           
            private void rifkeyPressed(KeyEvent e) 
            {
            }            
            private void nombrekeyPressed(KeyEvent e)
            {
                 if (e.VK_ENTER == e.getKeyCode()) 
                 {
                    formCooperativa.getTxtTelefonoC().requestFocusInWindow();
                 }
            }
            
            private void telefonokeyPressed(KeyEvent e) 
            {
               if (e.getSource().equals(formCooperativa.getTxtTelefonoC())) 
               {

                    String Cadena;
                    Cadena = formCooperativa.getTxtRifC().getText();

                    if (e.VK_ENTER == e.getKeyCode() && Cadena.length() != 11) 
                    {
                        Mensajes.Aviso("Telefono invalido", "");
                        formCooperativa.getTxtTelefonoC().requestFocusInWindow();

                    }
                }
            }

    @Override
    public void actionPerformed(ActionEvent e) 
    {
        if (e.getSource().equals(formCooperativa.getBtnSalirC())) 
        {
            formCooperativa.dispose();
        }

        if (e.getSource().equals(formCooperativa.getBtnBuscarC())) 
        {
            new ControladorConsulCooperativa(ListaCoop);
        }
        if (e.getSource().equals(formCooperativa.getBtnGuardarC())) 
        {
           // Grabar();
        }
        if (e.getSource().equals(formCooperativa.getBtnCancelarC())) 
        {
           // Limpiar();
          //  Enabled(false);
        }
        if (e.getSource().equals(formCooperativa.getBtnSeleccionRutas())) 
        {
          new ControladorRutaCoop(ListaCoop);
        }

    }
    
      public int ExisteChofer(String rifCoop) 
    {
        int i = 0, Posi = -1;
        
        Cooperativa coop;
        String RifLista;

       for (i = 0; i < ListaCoop.size(); i++) 
        {
            coop = ListaCoop.get(i);

            RifLista = coop.getRifC();

            if (RifLista.equals(rifCoop)) 
            {
                Posi = i;
                return Posi;
            }

        }

        return Posi;
    }

    @Override
    public void keyTyped(KeyEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void keyPressed(KeyEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void keyReleased(KeyEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}    

